/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package csBakery;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;

/*
 * 	@release Final Release
 *	@author  Chan Ka Yan
 *	@version 4.1.1
 *
 * 	@release Release 2
 *	@author  So Mo Kin
 *	@version 3.5.4
 *
 * 	@release Release 1
 *	@author  Chan Ka Yan
 *	@version 1.0.3
 */

public class LogController {
	private Log log;
	public static final String defaultLink = "log.txt";
	public final static String TYPE_SALE = "SALE";
	public final static String TYPE_STOCK = "STOCK";
	public LogController(){
		log = new Log(defaultLink);
	}
	
	public LogController(String link){
		log = new Log(link);
	}
	
	public void addLog(String name, String type, int qty, String date){
		//function for testing purpose
		log.addLog(new LogItem(name, type, qty, date));
	}
	
	public void addLog(String name, String type, int qty){
		Date thisDate = new Date();
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		String date = df.format(thisDate);
		log.addLog(new LogItem(name, type, qty, date));
	}
	
	public void printLog(){
		System.out.println(log);
	}
	
	public void saveLog(){
		log.writeLog();
	}
	public Vector<LogItem> getLog(){
		return log.getLog();
	}
	
	private class Log{
		private Vector<LogItem> vector;
		private final String defaultLink = "log.txt";
		private String thisLink;

		public Log(String link){
			thisLink = link;
			openLog(link);
		}
		private void openLog(String link){
			try{
				//read log file from existing log file
				vector = new Vector<LogItem>(10);
				File f = new File(link);
				if (!f.exists()){
					f.createNewFile();
				}
				FileInputStream fstream = new FileInputStream(f);
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String str = br.readLine();
				while (str != null){
					//trim the str and add logitem to log
					Vector<Integer> indexes = new Vector<Integer>();
					String temp = str;
					int index = temp.indexOf(' ');
					while(index != -1){
						indexes.add(index);
						index = temp.indexOf(' ', index+1);
					}
					
					String date, qty, type, name;
					if(indexes.size() > 3){
						name = str.substring(0, indexes.elementAt(indexes.size()-3));
						type = str.substring(indexes.elementAt(indexes.size()-3)+ 1, indexes.elementAt(indexes.size() -2));
						qty  = str.substring(indexes.elementAt(indexes.size()-2)+ 1, indexes.elementAt(indexes.size() -1));
						date = str.substring(indexes.elementAt(indexes.size()-1)+ 1);

					}
					else{
						name = str.substring(0, indexes.elementAt(0));
						type = str.substring(indexes.elementAt(0) + 1, indexes.elementAt(1));
						qty = str.substring(indexes.elementAt(1) + 1, indexes.elementAt(2));
						date = str.substring(indexes.elementAt(2)+1);
					}
					/* refactored, so that name contain space can be read
					 * int index = temp.indexOf(' ');
					String name = temp.substring(0, index);
					temp = temp.substring(index+1);
					index = temp.indexOf(' ');
					String type = temp.substring(0, index);
					temp = temp.substring(index+1);
					index = temp.indexOf(' ');
					String qty = temp.substring(0, index);
					temp = temp.substring(index+1);
					index = temp.indexOf(' ');
					String date = temp;*/
					
					LogItem itm = new LogItem(name, type, Integer.parseInt(qty), date);
					addLog(itm);
					str = br.readLine();
				}
				in.close();	 
			} catch (Exception e){
				System.out.println("Log Error: " + e.getMessage());
			}
		}
		public void writeLog(){
			//write existing log back to the log file
			try{
				File logFile = new File(thisLink);
				if(!logFile.exists())
					logFile.createNewFile();
				FileOutputStream fileStream = new FileOutputStream(logFile, false);
				byte[] myBytes = this.toString().getBytes();
				fileStream.write(myBytes);
				fileStream.close();
			} catch (Exception e){
				System.out.println("Write log error!");
			}
		}
		public String toString(){
			//return all the log in one String
			String toString = "";
			for (int i = 0; i < vector.size(); i++){
				toString = new String(toString + vector.elementAt(i).toString() + "\n");
			}
			return toString;
		}
		public void addLog(LogItem itm){
			vector.add(itm);
		}
		public Vector<LogItem> getLog(){
			return vector;
		}
	}
	public class LogItem{
		String product;
		String type;
		int Qty;
		String date;
		public LogItem(String name, String type, int qty, String date){
			product = name;
			this.type = type;
			Qty = qty;
			this.date = date;
		}
		public String toString(){
			return product + " " + type + " " + Qty + " " + date;
		}
		public String getName(){
			return product;
		}
		public String getType(){
			return type;
		}
		public int getQty(){
			return Qty;
		}
		public String getDate(){
			return date;
		}
	}
}
